package com.xgggh.redpack.ui.login;

import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.ViewModelProviders;

import android.view.View;
import android.widget.Toast;
import com.f2prateek.rx.preferences2.RxSharedPreferences;
import com.google.android.material.snackbar.Snackbar;
import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.xgggh.redpack.R;
import com.xgggh.redpack.domain.auth.Auth;

import javax.inject.Inject;

import dagger.android.AndroidInjection;
import io.reactivex.subscribers.DisposableSubscriber;
import model.Rsp;
import viewmodel.OauthViewModel;
import viewmodel.ViewModelFactory;

public class LogoutActivity extends AppCompatActivity {
    @Inject
    ViewModelFactory factory;
    @Inject
    RxSharedPreferences sharedPreferences;

    OauthViewModel model;
    @Inject
    Auth auth;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        AndroidInjection.inject(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_logout);
        model= ViewModelProviders.of(this,factory).get(OauthViewModel.class);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                logout();
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    private void logout(){
        model.logout()
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(this)))
                .subscribe(new DisposableSubscriber<Rsp>() {
            @Override
            public void onNext(Rsp rsp) {
                Toast.makeText(LogoutActivity.this, "-->"+rsp.msg, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onError(Throwable t) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

}
